const app = getApp()
// 语言
var util = require('../../utils/util.js');
// const cloud = require('wx-server-sdk');

Page({
  data: {
    showModal:false
  },
  onLoad: function (e) {
    if(e&&e.phone){
      this.setData({
        mustPhone:true
      })
    }
    
      this.salfLogin('init')
  },
  salfLogin(type){
    
    var  that=this;
    wx.request({
      url: app.globalData.urls + '/customer/login/wxindex',
      header: app.getRequestHeader(),
      data: {
        bdmin_user_id:util.share_bdmin_user_id()
      },
      success: function (res) {
        // && type!='init'
        if(res.data.code==200){
          that.wxLogin();
        }
        if(res.data.code==1100006){
          wx.hideLoading()
          let data=res.data.data;
          // util.getUserInfo(app,data);
          that.setData({
            data:data
          })
          wx.setStorage({
            data: data,
            key: 'customer_id',
          })
          if(data.head_url){
            wx.setStorage({
              key: "userInfo",
              data: {
                nickName: data.firstname,
                avatarUrl: data.head_url,
                customer_id: data.customer_id
              }
            })
          }
          if(!data.head_url||!data.phone){
            let getPhone=wx.getStorageSync('phone'),userInfo=wx.getStorageSync('userInfo')
              // && data.head_url=="" && !data.phone

              // 
            if(getPhone && getPhone.iv){
              console.log(2)
              wx.setStorageSync('isPhone', true)
              wx.setStorageSync('isLogin', 1)
              setTimeout(()=>{
                wx.navigateBack({
                  delta: 1
                });
              },20)
            }
          }
          let userInfo = wx.getStorageSync('userInfo')
          let phone=wx.getStorageSync('phone')
          if((data.head_url!="" || userInfo) && (data.phone || phone)){
            console.log(22222)
              wx.setStorageSync('isLogin', 1)
              if(!that.data.mustPhone){
                setTimeout(()=>{
                  wx.navigateBack({
                    delta: 1
                  });
                },20)
              }
          }

          app.saveReponseHeader(res);
        }
      }
    })
  },
  wxLogin: function(type){
    var that = this;
    wx.login({
      success: function (res) {
        that.setData({
          code:res.code
        })
        wx.request({
          url: app.globalData.urls + "/customer/login/wxcode",  // "/user/wxapp/login",
          header: app.getPostRequestHeader(),
          method: 'POST',
          data: {
            code: res.code,
            bdmin_user_id:util.share_bdmin_user_id()
          },
          success: function (res) {
            console.log('----r2323es.data.data',res.data.data)
            if(res.data.code==200){
              that.setData({
                session_key:res.data.data.session_key
              })
            }
            // if(type){
            //   wx.showLoading({
            //     title: "正在登录...",
            //   })
            // }
            wx.showLoading({
              title: "正在登录...",
            })
            console.log("---3-3--33")
            that.salfLogin(type);

            app.saveReponseHeader(res);
            
          }
        });
      }
    });
  },
  // 获取用户信息
  onGotUserInfo(e){
    var that = this;
    if (e.detail.errMsg == "getUserInfo:ok") {
      wx.setStorage({
        key: "userInfo",
        data: e.detail.userInfo
      })
      
      this.setData({ userInfo: e.detail.userInfo });
      if(!this.data.data.phone) that.showDialogBtn();//调用一键获取手机号弹窗（自己写的）
      else that.salfLogin();
      util.getUserInfo(app,that.data.data);
    }
  },
  // 隐藏一键获取手机号弹窗
  hideModal: function () {
    this.setData({
      showModal: false//修改弹窗状态为false,即隐藏
    });
  },
  getPhoneNumber(e){
    var that = this;
    that.hideModal();
    wx.setStorage({
      key: "phone",
      data: e.detail
    })
    console.log('---1111---')
    that.salfLogin();
    let customer_id = wx.getStorageSync('customer_id');
    that.recodePhone(customer_id,e.detail);
  },
  // 记录用户手机号
  recodePhone(data,getPhone){
    if(data.length==0) return;
    if(!getPhone.iv ) return;
    var that=this;
    console.log("--seesion-that.data",that.data)
      // wx.request({
      //   url: app.globalData.urls + "/order/get-session-key",  
      //   header: app.getPostRequestHeader(),
      //   method: 'POST',
      //   data: {
      //     code:that.data.code
      //   },
      //   success: function (res) {
      //     console.log("新街口",res.data)
      //     if(res.data.code==200){
             // 获取手机号信息
            wx.request({
              url: app.globalData.urls + "/order/update-phone",  
              header: app.getPostRequestHeader(),
              method: 'POST',
              data: {
                session_key:that.data.session_key,
                customer_id:data.customer_id,
                encryptedData:getPhone.encryptedData,
                iv:getPhone.iv
              },
              success: function (res) {
              }
            });
      //     }
      //   }
      // });
   
   
  },
   // 显示一键获取手机号弹窗
   showDialogBtn: function () {
    this.setData({
      showModal: true//修改弹窗状态为true，即显示
    })
  },
  onShow() {
  }
})